package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
        @Autowired
        private GraphInfoService graphInfoService;
        @Autowired
        private RoomAttrValueService roomAttrValueService;
        @Autowired
        private  RoomFacilityService roomFacilityService;
        @Autowired
        private RoomLabelService roomLabelService;
        @Autowired
        private RoomPaymentTypeService roomPaymentTypeService;
        @Autowired
        private RoomLeaseTermService roomLeaseTermService;
        @Autowired
        private RoomInfoMapper roomInfoMapper;
        @Autowired
        private ApartmentInfoMapper apartmentInfoMapper;
        @Autowired
        private GraphInfoMapper graphInfoMapper;
        @Autowired
        private RoomAttrValueMapper attrValueMapper;
        @Autowired
        private RoomFacilityMapper roomFacilityMapper;
        @Autowired
        private LabelInfoMapper labelInfoMapper;
        @Autowired
        private PaymentTypeMapper paymentTypeMapper;
        @Autowired
        private LeaseTermMapper leaseTermMapper;
    @Override
    public void saveOrUpdateVo(RoomSubmitVo roomSubmitVo) {
            super.saveOrUpdate(roomSubmitVo);
        if (roomSubmitVo.getId()!=null){
            //删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoService.remove(graphInfoQueryWrapper);
            //删除属性列表
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueQueryWrapper);
            //删除配套列表
            LambdaQueryWrapper<RoomFacility> roomFacilityQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityQueryWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityQueryWrapper);
            //删除标签列表
            LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(labelLambdaQueryWrapper);
            //删除支付列表
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
            //删除租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
        }

        //新增图片列表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
       if (!CollectionUtils.isEmpty(graphVoList)){
           ArrayList<GraphInfo> graphInfolist = new ArrayList<>();
           for (GraphVo graphVo:graphVoList) {
               GraphInfo graphInfo = new GraphInfo();
               graphInfo.setItemId(roomSubmitVo.getId());
               graphInfo.setItemType(ItemType.ROOM);
               graphInfo.setUrl(graphVo.getUrl());
               graphInfo.setName(graphVo.getName());
               graphInfolist.add(graphInfo);
           }
           graphInfoService.saveBatch(graphInfolist);
       }


        //新增属性列表
        List<Long> attrValueIdlist = roomSubmitVo.getAttrValueIds();
       if (!CollectionUtils.isEmpty(attrValueIdlist)){
           ArrayList<RoomAttrValue> roomAttrValuelist = new ArrayList<>();
           for (Long id:attrValueIdlist) {
               RoomAttrValue roomAttrValue = new RoomAttrValue();
               roomAttrValue.setRoomId(roomSubmitVo.getId());
               roomAttrValue.setAttrValueId(id);
               roomAttrValuelist.add(roomAttrValue);
           }
           roomAttrValueService.saveBatch(roomAttrValuelist);
       }

        //新增配套列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
       if (!CollectionUtils.isEmpty(facilityInfoIds)){
           ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
           for (Long id:facilityInfoIds) {
               RoomFacility roomFacility = new RoomFacility();
               roomFacility.setFacilityId(id);
               roomFacility.setRoomId(roomSubmitVo.getId());
               roomFacilities.add(roomFacility);
           }
           roomFacilityService.saveBatch(roomFacilities);
       }
        //新增标签列表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
       if (!CollectionUtils.isEmpty(labelInfoIds)){
           ArrayList<RoomLabel> roomLabels = new ArrayList<>();
           for (Long id:labelInfoIds) {
               RoomLabel roomLabel = new RoomLabel();
               roomLabel.setRoomId(roomSubmitVo.getId());
               roomLabel.setLabelId(id);
               roomLabels.add(roomLabel);
           }
           roomLabelService.saveBatch(roomLabels);
       }
        //新增支付列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
       if (!CollectionUtils.isEmpty(paymentTypeIds)){
           ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
           for (Long id : paymentTypeIds) {
               RoomPaymentType roomPaymentType = new RoomPaymentType();
               roomPaymentType.setRoomId(roomSubmitVo.getId());
               roomPaymentType.setPaymentTypeId(id);
               roomPaymentTypes.add(roomPaymentType);
           }
           roomPaymentTypeService.saveBatch(roomPaymentTypes);
       }

        //新增租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
       if (!CollectionUtils.isEmpty(leaseTermIds)){
           ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
           for (long id:leaseTermIds) {
               RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
               roomLeaseTerm.setRoomId(roomSubmitVo.getId());
               roomLeaseTerm.setLeaseTermId(id);
               roomLeaseTerms.add(roomLeaseTerm);
           }
           roomLeaseTermService.saveBatch(roomLeaseTerms);
       }


    }

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {

        return roomInfoMapper.pageItem(roomItemVoPage,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //图片列表
        List<GraphVo> graphVoList = graphInfoMapper.getGraphById(ItemType.ROOM, id);
        //属性列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectDetailById(id);
        //配套列表
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.selectDetailById(id);
        //标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectDetailById(id);
        //支付列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectDetailById(id);
        //租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectDetailById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        super.removeById(id);
        //删除图片列表
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoQueryWrapper);
        //删除属性列表
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueQueryWrapper);
        //删除配套列表
        LambdaQueryWrapper<RoomFacility> roomFacilityQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityQueryWrapper);
        //删除标签列表
        LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(labelLambdaQueryWrapper);
        //删除支付列表
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);
        //删除租期列表
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

    }


}




